var fs = require("fs");
var express = require("express");
// var multer = require('multer');

const { resourceUsage } = require("process");
var routers1 = express.Router();
var axios = require("axios");
const { router } = require("../../app");
const { log } = require("console");

const multipart = require("connect-multiparty");
const FormData = require("form-data");
var multipartMiddleware = multipart();

// 添加广告
routers1.put(
  "/advertisement/addAdvertisement",
  multipartMiddleware,
  (req, res) => {
    let advertisementName = req.query.advertisementName;
    let advertisementWeight = req.query.advertisementWeight;
    console.log(advertisementWeight);
    console.log(advertisementName);
    let file = req.files.File;
    console.log(file);
    console.log(file.headers);
    const uri = file.path;
    console.log("-----------------------------");
    console.log(uri);
    console.log("-----------------------------");
    let myFile = new FormData();
    myFile.append("file", fs.createReadStream(uri));
    axios({
      method: "put",
      url: `http://42.194.158.211:8081/video/advertisement/addAdvertisement?advertisementName=${encodeURI(
        advertisementName
      )}&advertisementWeight=${advertisementWeight}`,
      data: myFile,
      headers: myFile.getHeaders(),
      maxContentLength: Infinity,
      maxRedirects: 5,
      maxBodyLength: Infinity,
    })
      .then(function (data) {
        console.log("success============");
        console.log(data.data);
        res.send({ data: data.data });
      })
      .catch(function (err) {
        console.log("err");
        res.send({
          error: err,
          status: err.status,
        });
      });
  }
);
// 修改广告
routers1.put(
  "/advertisement/updateAdvertisementMsg",
  multipartMiddleware,
  (req, res) => {
    let advertisementName = req.query.advertisementName;
    let advertisementId = req.query.advertisementId;
    console.log(advertisementName);
    let file = req.files.File1;
    console.log(file);
    console.log(file.headers);
    const uri = file.path;
    console.log("-----------------------------");
    console.log(uri);
    console.log("-----------------------------");
    let myFile = new FormData();
    myFile.append("file", fs.createReadStream(uri));
    axios({
      method: "put",
      url: `http://42.194.158.211:8081/video/advertisement/updateAdvertisementMsg?advertisementName=${encodeURI(
        advertisementName
      )}&advertisementId=${advertisementId}`,
      data: myFile,
      headers: myFile.getHeaders(),
      maxContentLength: Infinity,
      maxRedirects: 5,
      maxBodyLength: Infinity,
    })
      .then(function (data) {
        console.log("success============");
        console.log(data.data);
        res.send({ data: data.data });
      })
      .catch(function (err) {
        console.log("err");
        res.send({
          error: err,
          status: err.status,
        });
      });
  }
);

// 添加视频
routers1.post("/video/videoCRUD/addVideo", multipartMiddleware, (req, res) => {
  let file = req.files.File;
  let gif = req.files.Gif;
  let photo = req.files.photo;
  const uri1 = file.path;
  const uri2 = gif.path;
  const uri3 = photo.path;
  let myFile = new FormData();
  myFile.append("file", fs.createReadStream(uri1));
  myFile.append("gif", fs.createReadStream(uri2));
  myFile.append("photo", fs.createReadStream(uri3));
  axios({
    method: "post",
    url: `http://42.194.158.211:8081/video/videoCRUD/addVideo?introduction=${encodeURI(
      req.query.introduction
    )}&userid=1&videoName=${encodeURI(
      req.query.videoName
    )}&videoType.regionType=${encodeURI(
      req.query.area
    )}&videoType.videoBriefly=${encodeURI(
      req.query.videoBriefly
    )}&videoType.videoTags=${encodeURI(
      req.query.videoTags
    )}&videoType.videoType=${encodeURI(
      req.query.type
    )}&videoType.videoYear=${encodeURI(
      "_" + req.query.year
    )}&videoType.whetherFree=${encodeURI(req.query.whetherFree)}`,
    data: myFile,
    headers: myFile.getHeaders(),
    maxContentLength: Infinity,
    maxRedirects: 5,
    maxBodyLength: Infinity,
  })
    .then(function (data) {
      console.log("success============");
      console.log(data.data);
      res.send({ data: data.data });
    })
    .catch(function (err) {
      console.log("err");
      res.send({
        error: err,
        status: err.status,
      });
    });
});
// 修改视频
routers1.put("/video/videoCRUD/updateVideo", (req, res) => {
  console.log(req.query);
  axios({
    method: "put",
    url: `http://42.194.158.211:8081/video/videoCRUD/updateVideo`,
    params: {
      area: req.query.area,
      introduction: req.query.introduction,
      type: req.query.type,
      videoId: req.query.videoId,
      videoName: req.query.videoName,
      videoTags: req.query.videoTags,
      whetherFree: req.query.whetherFree,
      year: req.query.year,
    },
  })
    .then(function (data) {
      res.status(200).json({
        data: data.data,
        code: 0,
      });
    })
    .catch(function (err) {
      console.log(err);
      res.status(500).json({
        msg: "失败",
      });
    });
});

// 上传演员封面

routers1.put("/actor/updateActorSculpture", multipartMiddleware, (req, res) => {
  console.log(req.query.actorId);
  let file = req.files.File;
  const uri = file.path;
  console.log(uri);
  let myFile = new FormData();
  myFile.append("File", fs.createReadStream(uri));
  axios({
    method: "put",
    url: `http://42.194.158.211:8081/video/actor/updateActorSculpture?actor=${req.query.actorId}`,
    data: myFile,
    headers: myFile.getHeaders(),
    maxContentLength: Infinity,
    maxRedirects: 5,
    maxBodyLength: Infinity,
  })
    .then(function (data) {
      console.log("success============");
      console.log(data.data);
      res.send({ data: data.data });
    })
    .catch(function (err) {
      console.log(err);
      res.send({
        error: err,
        status: err.status,
      });
    });
});
module.exports = routers1;
